package com.javaweb.servlet.action;

import com.javaweb.servlet.utils.DBUtil;
import com.javaweb.servlet.bean.User;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        System.out.println("niaho");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out= response.getWriter();

        String username=request.getParameter("name");
        String password=request.getParameter("password");
        if(username==""||username.length()==0){
            request.setAttribute("namemsg","用户名为空!");
            request.getRequestDispatcher("index.jsp").forward(request, response);
        }
        if(password==""||password.length()==0){
            request.setAttribute("pwdmsg","密码为空!");
            request.getRequestDispatcher("index.jsp").forward(request, response);
        }
        //链接数据库
        Connection conn=null;
        PreparedStatement ps =null;
        ResultSet rs = null;
        boolean success=false;
        try {
            /*获取链接*/
            conn = DBUtil.getConnection();
            /*获取预编译的数据库操作对象*/
            String sql ="select password  from t_student where account=?";
            ps=conn.prepareStatement(sql);
            ps.setString(1,username);//1就是给sql这条语句中的第一个?号赋值
            /*执行Sql语句*/
            rs=ps.executeQuery();
            /*处理结果集*/
            if (rs.next())
            {
                String pwd = rs.getString("password");
                if(!pwd.equals(password)){
                    request.setAttribute("pwdmsg","密码不正确!");
                    request.getRequestDispatcher("/index.jsp").forward(request, response);
                }else{
                    // 如果验证成功，则转发succeed.jsp页面，并在页面显示用户名
                    success=true;
                    ServletContext application = request.getServletContext();
                    // 将数据添加到全局作用域对象，作为共享数据
                    application.setAttribute("name", username);
                    application.setAttribute("password",password);
                }
            } else
            {
                // 如果验证失败，则重定向到登录页面
                request.setAttribute("namemsg","用户名不正确");

                request.getRequestDispatcher("index.jsp").forward(request, response);
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                /*释放资源*/
                DBUtil.close(conn,ps,rs);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (success) {
            //获取session
            HttpSession session=request.getSession();
            //session.setAttribute("username",username);
            User user=new User(username,password);
            session.setAttribute("user",user);
            response.sendRedirect(request.getContextPath()+"/loginSucess.jsp");
        }else{
            response.sendRedirect(request.getContextPath()+"/index.jsp");
        }

    }
}
